<?php
	include_once("../../defines.php");

	FunctionsHelper::includeFiles(
		array(
			PUBLIC_CONTROLLERS_DIRECTORY . 'news.php',
			PUBLIC_MODELS_DIRECTORY . 'model.php',
			PUBLIC_MODELS_DIRECTORY . 'news.php',
			PUBLIC_MODELS_DIRECTORY . 'user.php',
			PUBLIC_MODELS_DIRECTORY . 'comment.php'
		)
	);
	
	switch ($_POST["operation"]){
		case "read":
			$news = new News(
				array(
					"id" => $_POST["news_id"]
				)
			);
		
			$news->getTheComments($_POST["currentPage"], COMMENTS_PER_PAGE);
		
			$html = "";
			foreach ($news->getComments() as $comment){
				$email = $comment->getEmail();
				$html .= '<div class="comment">';
				$html .= '	<div class="commentContent commentHeader">';
				$html .= '		<span class="name">' . htmlentities(utf8_decode($comment->getWritter())) . ' </span><span class="email">' . (!empty($email) ? ' - ' . $email : ''). '</span>';
				//$html .= '		<span>Escrito por: </span>' . $comment->getWritter() . ' ' . (!empty($email) ? $comment->getEmail() : '');
				$html .= '		<span class="date">' . date("d/m/Y H:i:s", $comment->getDate()) . ' hs.</span>';
				//$html .= '		<span>Fecha: </span>' . date("d/m/Y H:i:s", $comment->getDate());
				$html .= '	</div>';
				$html .= '	<div class="commentContent commentBody">';
				$html .= 		htmlentities(utf8_decode($comment->getComment()));
				$html .= '	</div>';
				$html .= '</div>';
			}
			echo json_encode(array("text" => $html, "totalComments" => $news->getTotalComments()));
		break;
		case "publish":
			$NewsC = new NewsC();
			echo $NewsC->PublishComment($_POST);
		break;
	}
	
?>